package com.scwatch.core.web.back;

import com.scwatch.common.web.Servlets;
import com.scwatch.core.constant.Constants;
import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.Special;
import com.scwatch.core.domain.SpecialCategory;
import com.scwatch.core.service.ModelService;
import com.scwatch.core.service.OperationLogService;
import com.scwatch.core.service.SpecialCategoryService;
import com.scwatch.core.service.SpecialService;
import com.scwatch.core.support.Backends;
import com.scwatch.core.support.CmsException;
import com.scwatch.core.support.Context;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static com.scwatch.core.constant.Constants.DELETE_SUCCESS;
import static com.scwatch.core.constant.Constants.MESSAGE;

/**
 * 专题回收站
 */
@Controller
@RequestMapping("/core/special_recycle")
public class SpecialRecycleController {
    private static final Logger logger = LoggerFactory
            .getLogger(SpecialController.class);

    @RequiresPermissions("core:special_recycle:list")
    @RequestMapping("list.do")
    public String list(@PageableDefault(sort = "id", direction = Sort.Direction.DESC) Pageable pageable,
            HttpServletRequest request, org.springframework.ui.Model modelMap) {
        Integer siteId = Context.getCurrentSiteId();
        Map<String, String[]> params = Servlets.getParamValuesMap(request,
                Constants.SEARCH_PREFIX);
        Page<Special> pagedList = service.findAll(siteId, params, pageable,1);
        modelMap.addAttribute("pagedList", pagedList);

        List<SpecialCategory> categoryList = specialCategoryService.findList(siteId);
        modelMap.addAttribute("categoryList", categoryList);
        return "core/special/special_recycle";
    }

    @RequiresPermissions("core:special_recycle:delete")
    @RequestMapping("delete.do")
    public String delete(Integer[] ids, HttpServletRequest request, RedirectAttributes ra) {
        Site site = Context.getCurrentSite();
        validateIds(ids, site.getId());
        Special[] beans = service.delete(ids);
        for (Special bean : beans) {
            logService.operation("opr.special.delete", bean.getTitle(), null,
                    bean.getId(), request);
            logger.info("delete Special, title={}.", bean.getTitle());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

//    @RequiresPermissions("core:special:delete")
//    @RequestMapping("updateDelStatus.do")
//    public String updateDelStatus(Integer[] ids, HttpServletRequest request, RedirectAttributes ra) {
//        Site site = Context.getCurrentSite();
//        validateIds(ids, site.getId());
//        Special[] beans = service.updateDelStatus(ids,0);
//        for (Special bean : beans) {
//            logService.operation("opr.special.updateDelStatus", bean.getTitle(), null,
//                    bean.getId(), request);
//            logger.info("updateDelStatus Special, title={}.", bean.getTitle());
//        }
//        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
//        return "redirect:list.do";
//    }

    @RequestMapping("updateDelStatus.do")
    @RequiresRoles("super")
    @RequiresPermissions("core:special_recycle:updateDelStatus")
    public String updateDelStatus(Integer[] ids, HttpServletRequest request, RedirectAttributes ra) {
        for (Integer id : ids) {
            // ID==1的默认站点不能删除
            if (id == 1) {
                throw new CmsException("special.error.defaultSiteCannotBeDeleted");
            }
        }
        Special[] beans = service.updateDelStatus(ids,0);
        for (Special bean : beans) {
            logService.operation("opr.special.updateDelStatus", bean.getTitle(), null, bean.getId(), request);
            logger.info("updateDeleteStatus, name={}.", bean.getTitle());
        }
        ra.addFlashAttribute(MESSAGE, DELETE_SUCCESS);
        return "redirect:list.do";
    }

    private void validateIds(Integer[] ids, Integer siteId) {
        for (Integer id : ids) {
            Backends.validateDataInSite(service.get(id), siteId);
        }
    }

    @Autowired
    private SpecialService service;
    @Autowired
    private SpecialCategoryService specialCategoryService;
    @Autowired
    private OperationLogService logService;
    @Autowired
    private ModelService modelService;
}
